<!DOCTYPE HTML>
<html>

<head>
    <title>three.proton - helloworld</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
    <style type="text/css">
    body {
        font-family: Monospace;
        background-color: #fff;
        margin: 0;
        padding: 0;
        overflow: hidden;
    }
    </style>
</head>

<body>
    <div id="container"></div>
    <script src="../lib/stats.min.js"></script>
    <script src="../lib/three.min.js"></script>
    <script src="../build/three.proton.min.js"></script>
    <script src="./js/lib/TrackballControls.js"></script>
    <script>
    var controls, camera, scene, renderer, proton;
    var cameraCube, sceneCube;
    var textureEquirec, textureCube, textureSphere;
    var cubeMesh, sphereMesh;
    var sphereMaterial;
    var refract;

    init();
    function init() {
        initScene();
        addControls();
        createProton();
        animate();
    }

    function initScene() {
        // CAMERAS
        camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 1, 100000);
        camera.position.set(0, 0, 1000);
        cameraCube = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 1, 100000);

        // SCENE
        scene = new THREE.Scene();
        sceneCube = new THREE.Scene();
        // Lights
        var ambient = new THREE.AmbientLight(0xffffff);
        scene.add(ambient);
        // Textures
        var r = "img/cube/";
        var urls = [r + "posx.jpg", r + "negx.jpg",
            r + "posy.jpg", r + "negy.jpg",
            r + "posz.jpg", r + "negz.jpg"
        ];
        textureCube = new THREE.CubeTextureLoader().load(urls);
        textureCube.format = THREE.RGBFormat;
        textureCube.mapping = THREE.CubeReflectionMapping;
        var textureLoader = new THREE.TextureLoader();
        textureEquirec = textureLoader.load("img/cube/2294472375_24a3b8ef46_o.jpg");
        textureEquirec.mapping = THREE.EquirectangularReflectionMapping;
        textureEquirec.magFilter = THREE.LinearFilter;
        textureEquirec.minFilter = THREE.LinearMipMapLinearFilter;
        textureSphere = textureLoader.load("img/cube/metal.jpg");
        textureSphere.mapping = THREE.SphericalReflectionMapping;
        // Materials
        var equirectShader = THREE.ShaderLib["equirect"];
        var equirectMaterial = new THREE.ShaderMaterial({
            fragmentShader: equirectShader.fragmentShader,
            vertexShader: equirectShader.vertexShader,
            uniforms: equirectShader.uniforms,
            depthWrite: false,
            side: THREE.BackSide
        });
        equirectMaterial.uniforms["tEquirect"].value = textureEquirec;
        var cubeShader = THREE.ShaderLib["cube"];
        var cubeMaterial = new THREE.ShaderMaterial({
            fragmentShader: cubeShader.fragmentShader,
            vertexShader: cubeShader.vertexShader,
            uniforms: cubeShader.uniforms,
            depthWrite: false,
            side: THREE.BackSide
        });
        cubeMaterial.uniforms["tCube"].value = textureCube;
        // Skybox
        cubeMesh = new THREE.Mesh(new THREE.BoxGeometry(100, 100, 100), cubeMaterial);
        sceneCube.add(cubeMesh);
        //
        var geometry = new THREE.SphereGeometry(100.0, 24, 24);
        sphereMaterial = new THREE.MeshLambertMaterial({
            envMap: textureCube
        });
        sphereMesh = new THREE.Mesh(geometry, sphereMaterial);
        //scene.add(sphereMesh);
        //
        renderer = new THREE.WebGLRenderer();
        renderer.autoClear = false;
        renderer.setPixelRatio(window.devicePixelRatio);
        renderer.setSize(window.innerWidth, window.innerHeight);
        renderer.setFaceCulling(THREE.CullFaceNone);
        document.body.appendChild(renderer.domElement);

        window.addEventListener('resize', onWindowResize, false);
    }

    function addControls() {
        controls = new THREE.TrackballControls(camera);
        controls.rotateSpeed = 1.0;
        controls.zoomSpeed = 1.2;
        controls.panSpeed = 0.8;
        controls.noZoom = false;
        controls.noPan = false;
        controls.staticMoving = true;
        controls.dynamicDampingFactor = 0.3;
    }

    function createProton() {
        proton = new Proton;
        var emitter = new Proton.Emitter();
        emitter.rate = new Proton.Rate(new Proton.Span(2, 5), new Proton.Span(.5, 1));
        emitter.addInitialize(new Proton.Mass(1));
        emitter.addInitialize(new Proton.Radius(100));
        emitter.addInitialize(new Proton.Life(5, 6));
        emitter.addInitialize(new Proton.Body(sphereMesh));
        emitter.addInitialize(new Proton.V(new Proton.Span(300, 500), new Proton.Vector3D(0, 1, 0), 30));
        //emitter.addBehaviour(new Proton.Alpha(1, 0));
        emitter.addBehaviour(new Proton.Scale(1));
        emitter.addBehaviour(new Proton.Gravity(4));
        emitter.addBehaviour(new Proton.Collision(emitter));

        emitter.emit();
        proton.addEmitter(emitter);
        proton.addRender(new Proton.MeshRender(scene));
    }

    function onWindowResize() {
        camera.aspect = window.innerWidth / window.innerHeight;
        camera.updateProjectionMatrix();
        cameraCube.aspect = window.innerWidth / window.innerHeight;
        cameraCube.updateProjectionMatrix();
        renderer.setSize(window.innerWidth, window.innerHeight);
    }
    //
    function animate() {
        requestAnimationFrame(animate);
        render();
        controls.update();
    }

    function render() {
        var timer = -0.0002 * Date.now();
        camera.lookAt(scene.position);
        cameraCube.rotation.copy(camera.rotation);
        renderer.render(sceneCube, cameraCube);
        renderer.render(scene, camera);
        proton.update();
    }
    </script>
</body>

</html>
